package com.example.mtusbapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.mt.android.usbsdk.bean.ErroCode;
import com.mt.android.usbsdk.bean.ReciveMsg;
import com.mt.android.usbsdk.help.MtUsbHelper;
import com.mt.android.usbsdk.help.UsbConnectCallBack;
import com.mt.android.usbsdk.help.UsbListenCallBack;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText sendCommonMsg;
    private EditText sendCommonMsg2;
    private EditText setBaudRateTxt;
    private Button setBaudRatBtn;
    private Button connectUsbBtn;
    private Button sendCommonBtn;
    private Button sendCommonBtn2;
    private Button startReadDataBtn;
    private Button stopReadDataBtn;
    private Button clearReadDataLog;

    private ScrollView mScrollView;
    private TextView mDumpTextView;
    private MtUsbHelper mMtUsbHelper;

    private UsbListenCallBack mUsbListenCallBack = new UsbListenCallBack(){

        @Override
        public void onDatasRecive(ReciveMsg tReciveMsg) {
            Message msg = new Message();
            msg.what = 2;
            msg.obj = tReciveMsg.getReciveDataString();
            mHandler.handleMessage(msg);
        }
    };

    private UsbConnectCallBack mUsbConnectCallBack = new UsbConnectCallBack(){

        @Override
        public void onSuccess() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(MainActivity.this,"连接成功",Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }).start();
        }

        @Override
        public void onFail(final ErroCode ErroCode) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Looper.prepare();
                    Toast.makeText(MainActivity.this,ErroCode.getMsg(),Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            }).start();
        }
    };


    //在主线程里面处理消息并更新UI界面
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    String readMsg = "Send Msg:" + (String) msg.obj + "\n\n";
                    mDumpTextView.append(readMsg);
                    mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
                    break;
                case 2:
                    String sendMsg = "Read Msg:" + (String) msg.obj + "\n\n";
                    mDumpTextView.append(sendMsg);
                    mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }


    private void initView() {
        sendCommonMsg = this.findViewById(R.id.sendCommonMsg);
        sendCommonMsg2 = this.findViewById(R.id.sendCommonMsg2);
        setBaudRateTxt = this.findViewById(R.id.setBaudRateTxt);
        setBaudRatBtn = this.findViewById(R.id.setBaudRatBtn);
        connectUsbBtn = this.findViewById(R.id.connectUsbBtn);
        sendCommonBtn = this.findViewById(R.id.sendCommonBtn);
        sendCommonBtn2 = this.findViewById(R.id.sendCommonBtn2);
        startReadDataBtn = this.findViewById(R.id.startReadDataBtn);
        stopReadDataBtn = this.findViewById(R.id.stopReadDataBtn);
        clearReadDataLog = this.findViewById(R.id.clearReadDataLog);

        mScrollView = this.findViewById(R.id.demoScroller);
        mDumpTextView = this.findViewById(R.id.consoleText);
    }

    private void initData() {
        mMtUsbHelper=MtUsbHelper.getInstance();
        mMtUsbHelper.setMUsbConnectCallBack(mUsbConnectCallBack);
        mMtUsbHelper.setMUsbListenCallBack(mUsbListenCallBack);
        sendCommonBtn.setOnClickListener(this);
        sendCommonBtn2.setOnClickListener(this);
        setBaudRatBtn.setOnClickListener(this);
        connectUsbBtn.setOnClickListener(this);
        startReadDataBtn.setOnClickListener(this);
        stopReadDataBtn.setOnClickListener(this);
        clearReadDataLog.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Message msg = new Message();
        switch (view.getId()) {
            case R.id.sendCommonBtn:
                String sendCommonMsgStr = sendCommonMsg.getText().toString();
                msg.what = 1;
                msg.obj = sendCommonMsgStr;
                mHandler.handleMessage(msg);
                mMtUsbHelper.sendMsg(sendCommonMsgStr);
                break;
            case R.id.sendCommonBtn2:
                String sendCommonMsg2Str = sendCommonMsg2.getText().toString();
                msg.what = 1;
                msg.obj = sendCommonMsg2Str;
                mHandler.handleMessage(msg);
                mMtUsbHelper.sendMsg(sendCommonMsg2Str);
                break;
            case R.id.setBaudRatBtn:
                mMtUsbHelper.setBaudRate(Integer.parseInt(setBaudRateTxt.getText().toString()));
                break;
            case R.id.connectUsbBtn:
                mMtUsbHelper.usbDriverConnect(this);
                break;
            case R.id.startReadDataBtn:
                mMtUsbHelper.startReadMsg();
                break;
            case R.id.stopReadDataBtn:
                mMtUsbHelper.stopReadMsg();
                break;
            case R.id.clearReadDataLog:
                mDumpTextView.setText("");
                mScrollView.smoothScrollTo(0, mDumpTextView.getBottom());
                break;
        }
    }
}